import json
import django.utils.timezone as timezone

from django.contrib.auth.models import User
from django.db import models

from django.db.backends.mysql.base import DatabaseFeatures # 关键设置
DatabaseFeatures.supports_microsecond_precision = False # 关键设置


# Create your models here.
from django.utils.html import format_html

leixings=(("tou","头"),("jian","肩"),("yao","腰"),("kuzi","裤"),("xie","鞋"),("shou","手"),
         ("yi","衣"),('jiezhizuo',"戒指左"),("jiezhiyou","戒指右"),("xianglian",'项链'),
         ("hufu",'护符'),("zuoji","坐骑"),("wuqizuo","武器左"),('wuqiyou',"武器右"),("pifeng","披风"))
zhiyes = (("xiake", "侠客"), ("xiuzhen", "修真"), ("zhanshi", "战士"), ("fashi", "法师"),
          ("xianzhe", "贤者"), ("qiangwang", "枪王"), ("wusheng", '武圣'))
types = (("zhili", "智力"), ("liliang", "力量"), ("ganzhi", "感知"), ("minjie", "敏捷"))
class EX1(models.Model):
    title=models.CharField(max_length=30,verbose_name="标题")
    content=models.TextField(max_length=400,verbose_name="内容")

class jineng(models.Model):
    zhiyes=(("xiake","侠客"),("xiuzhen","修真"),("zhanshi","战士"),("fashi","法师"),
            ("xianzhe","贤者"),("qiangwang","枪王"),("wusheng",'武圣'))
    name=models.CharField(max_length=20,verbose_name="技能名称")
    zhiye=models.CharField(max_length=20,choices=zhiyes,verbose_name="职业")
    class Meta:
        verbose_name = '技能表'
        verbose_name_plural = '技能表'

    def __str__(self):
        return self.name

    @property
    def attrs_content(self):
        '''
        获取技能所属属性
        :return:str
        '''
        content=""
        for item in self.attrs.all():
            if item.attr_value != int(item.attr_value):
                value=format(item.attr_value, ".0%")
            else:
                value=int(item.attr_value)
            content+=f"{item.attr_name.name} {value}<br>"

        return content
    @property
    def zhiye_name(self):
        '''
        获取职业名称
        :return:
        '''
        for item in self.zhiyes:
            if self.zhiye in item:
                return item[1]

class zhuangbei(models.Model):

    name=models.CharField(max_length=20,verbose_name="装备名称")
    leixing=models.CharField(max_length=20,verbose_name="装备类型",choices=leixings)

    class Meta:
        verbose_name = '装备表'
        verbose_name_plural = '装备表'

    def __str__(self):
        return self.name

    @property
    def attrs_content(self):
        '''
        获取装备属性内容
        :return: str
        '''
        content=""
        for item in self.zhuangbei_attrs.all():
            if item.attr_value != int(item.attr_value):
                value=format(item.attr_value, ".0%")
            else:
                value=int(item.attr_value)
            content+=f"{item.attr_name.name} {value}<br>"

        return content
    @property
    def type_name(self):
        for item in leixings:
            if self.leixing in item:
                return item[1]

class chenghao(models.Model):
    # 称号名称
    name=models.CharField(max_length=20,verbose_name="称号名称")
    class Meta:
        verbose_name = '称号表'
        verbose_name_plural = '称号表'

    def __str__(self):
        return self.name


class renwu(models.Model):
    user=models.OneToOneField(User, on_delete=models.CASCADE,
                                related_name='game_user',default=1)


    # 人物昵称
    name = models.CharField(max_length=20, verbose_name="人物昵称")
    # 人物职业
    zhiye = models.CharField(max_length=20, choices=zhiyes, verbose_name="职业")

    # 人物属性点
    nums=models.IntegerField(verbose_name="属性点",default=0)

    # 宝石\附魔
    nums_bf=models.IntegerField(verbose_name="宝石、附魔增加属性点",default=0)
    # 加点类型
    nums_type=models.CharField(max_length=10,choices=types,verbose_name="属性点类型",default="zhili")

    chenghao=models.ForeignKey(chenghao,on_delete=models.SET_NULL,null=True)


    class Meta:
        verbose_name = '人物表'
        verbose_name_plural = '人物表'

    def __str__(self):
        return self.name

    @property
    def nums_type_name(self):
        for item in types:
            if self.nums_type in item:
                return item[1]
    @property
    def zhiye_name(self):
        for item in zhiyes:
            if self.zhiye in item:
                return item[1]


class jineng_attrs(models.Model):
    # 技能属性名称
    name=models.CharField(max_length=20,verbose_name="属性名称")
    # 是增加的值还是百分比增幅
    is_zengfu=models.BooleanField(verbose_name="是否为百分比增幅")

    class Meta:
        verbose_name = '属性项目表'
        verbose_name_plural = '属性项目表'

    def __str__(self):
        return self.name

class jineng_key_value(models.Model):
    # 关联技能表
    jineng=models.ForeignKey(jineng,on_delete=models.CASCADE,related_name="attrs")
    # 关联技能项表
    attr_name=models.ForeignKey(jineng_attrs,on_delete=models.CASCADE,related_name="attr_name")
    # 项目增加的技能值
    attr_value=models.FloatField(verbose_name="增加值")
    class Meta:
        verbose_name = '技能属性_值表'
        verbose_name_plural = '技能属性_值表'

class chenghao_key_value(models.Model):
    chenghao=models.ForeignKey(chenghao,on_delete=models.CASCADE)
    attr_name=models.ForeignKey(jineng_attrs,on_delete=models.CASCADE,related_name="ch_attr_name")
    attr_value=models.FloatField(verbose_name="增加值")
    class Meta:
        verbose_name = '称号属性_值表'
        verbose_name_plural = '称号属性_值表'
    def __str__(self):
        return self.chenghao.name


class zhuangbei_key_value(models.Model):
    # 关联装备表
    zhuangbei=models.ForeignKey(zhuangbei,on_delete=models.CASCADE,related_name="zhuangbei_attrs")
    # 关联属性项表
    attr_name=models.ForeignKey(jineng_attrs,on_delete=models.CASCADE,related_name="zhuangbei_attr_name")
    # 项目增加的值
    attr_value=models.FloatField(verbose_name="增加值")

    class Meta:
        verbose_name = '装备属性_值表'
        verbose_name_plural = '装备属性_值表'

    def __str__(self):
        return self.zhuangbei.name


class renwu_jieng(models.Model):
    # 人物 技能 绑定表
    renwu=models.ForeignKey(renwu,on_delete=models.CASCADE)

    jineng=models.ForeignKey(jineng,on_delete=models.CASCADE)

    class Meta:
        verbose_name = '人物技能绑定表'
        verbose_name_plural = '人物技能绑定表'

class renwu_zhuangbei(models.Model):
    # 人物 技能 绑定表
    renwu = models.ForeignKey(renwu, on_delete=models.CASCADE)

    zhuangbei = models.ForeignKey(zhuangbei, on_delete=models.CASCADE)

    class Meta:
        verbose_name = '人物装备绑定表'
        verbose_name_plural = '人物装备绑定表'

class Submit_ZhuangBei(models.Model):
    # 提交装备和装备属性
    name=models.CharField(max_length=20,verbose_name="装备名称")

    leixing = models.CharField(max_length=20, verbose_name="装备类型", choices=leixings)

    attrs_json=models.CharField(max_length=1000, verbose_name="装备属性json数据")

    is_save=models.BooleanField(verbose_name="已经录入装备库",default=False)

    add_time=models.DateField(auto_now_add=True,verbose_name="提交时间",null=True)

    class Meta:
        verbose_name = '装备提交申请表'
        verbose_name_plural = '装备提交申请表'

    def attrs_str(self):
        strs=[]
        data=json.loads(self.attrs_json)
        for item in data['attr_data']:
            strs.append(format_html('<span>{}:{}</br></span>',item['name'],item['value']))

        htmlstr="<div>"+"{}"*len(strs)+"</div>"
        return format_html(htmlstr,*strs)

    def pass_audit_str(self):

        if self.is_save:
            return "审核通过"
        btn_str = '<a class="btn btn-xs btn-danger" href="{}">' \
                  '<input name="通过审核"' \
                  'type="button" id="passButton" ' \
                  'title="passButton" value="通过审核">' \
                  '</a>'
        return format_html(btn_str, '/pass_audit/{}'.format(self.pk))



class renwuattrs(models.Model):
    # 绑定游戏角色
    renwu=models.ForeignKey(renwu,on_delete=models.CASCADE,related_name="renwuattr")

    # 力量
    liliang=models.IntegerField(verbose_name="力量",default=0)
    # 敏捷
    minjie=models.IntegerField(verbose_name="敏捷",default=0)
    # 智力
    zhili=models.IntegerField(verbose_name="智力",default=0)
    # 感知
    ganzhi = models.IntegerField(verbose_name="感知",default=0)


    class Meta:
        verbose_name = '人数属性表'
        verbose_name_plural = '人数属性表'

    def __str__(self):
        return self.renwu.name

    # # 劈砍攻击力
    # pikan=models.IntegerField(verbose_name="劈砍")
    # # 穿刺攻击力
    # chuanci=models.IntegerField(verbose_name="穿刺")
    # # 魔法攻击力
    # mogong=models.IntegerField(verbose_name="魔攻")
    # # 攻次
    # gongci=models.IntegerField(verbose_name="攻次")
    # # 破甲
    # pojia = models.IntegerField(verbose_name="破甲")
    # # 魔命
    # moming = models.IntegerField(verbose_name="魔命")
    # # 致命
    # zhiming=models.IntegerField(verbose_name="致命")
    # # 洞察
    # dongcha=models.IntegerField(verbose_name="洞察")
    #
    # # 出手
    # chushou = models.IntegerField(verbose_name="出手")
    # # 躲闪
    # duoshan = models.IntegerField(verbose_name="躲闪")
    #
    # # 无视魔法反馈
    # wushimofan = models.IntegerField(verbose_name="无视魔法反馈")